package com.apikey.auth.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.ArrayList;
import java.util.List;

/**
 * API Key管理框架配置属性类
 * 提供完整的配置选项，支持通过application.yml进行配置
 *
 * @author API Key Framework
 * @version 1.0
 */
@ConfigurationProperties(prefix = "apikey")
public class ApiKeyProperties {

    /**
     * 是否启用API Key管理框架
     */
    private boolean enabled = true;

    /**
     * 拦截器配置
     */
    private Interceptor interceptor = new Interceptor();

    /**
     * Web管理界面配置
     */
    private Web web = new Web();

    /**
     * 管理接口配置
     */
    private Management management = new Management();

    /**
     * 安全配置
     */
    private Security security = new Security();

    /**
     * 监控配置
     */
    private Monitoring monitoring = new Monitoring();

    /**
     * 数据库配置
     */
    private Database database = new Database();

    // Getters and Setters
    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public Interceptor getInterceptor() {
        return interceptor;
    }

    public void setInterceptor(Interceptor interceptor) {
        this.interceptor = interceptor;
    }

    public Web getWeb() {
        return web;
    }

    public void setWeb(Web web) {
        this.web = web;
    }

    public Management getManagement() {
        return management;
    }

    public void setManagement(Management management) {
        this.management = management;
    }

    public Security getSecurity() {
        return security;
    }

    public void setSecurity(Security security) {
        this.security = security;
    }

    public Monitoring getMonitoring() {
        return monitoring;
    }

    public void setMonitoring(Monitoring monitoring) {
        this.monitoring = monitoring;
    }

    public Database getDatabase() {
        return database;
    }

    public void setDatabase(Database database) {
        this.database = database;
    }

    /**
     * 拦截器配置类
     */
    public static class Interceptor {
        /**
         * 是否启用拦截器
         */
        private boolean enabled = true;

        /**
         * 拦截器执行顺序
         */
        private int order = 100;

        /**
         * 需要拦截的路径模式
         */
        private List<String> includePatterns = new ArrayList<String>() {{
            add("/api/**");
        }};

        /**
         * 需要排除的路径模式
         */
        private List<String> excludePatterns = new ArrayList<String>() {{
            add("/api/auth/**");
            add("/api/public/**");
            add("/actuator/**");
            add("/error");
        }};

        /**
         * API Key请求头名称
         */
        private String headerName = "X-API-Key";

        /**
         * API Key请求参数名称
         */
        private String paramName = "apiKey";

        /**
         * 是否允许通过请求参数传递API Key
         */
        private boolean allowParameter = false;

        /**
         * 是否记录访问日志
         */
        private boolean logAccess = true;

        /**
         * 是否启用速率限制
         */
        private boolean rateLimitEnabled = false;

        /**
         * 每分钟最大请求数
         */
        private int maxRequestsPerMinute = 60;

        // Getters and Setters
        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public int getOrder() {
            return order;
        }

        public void setOrder(int order) {
            this.order = order;
        }

        public List<String> getIncludePatterns() {
            return includePatterns;
        }

        public void setIncludePatterns(List<String> includePatterns) {
            this.includePatterns = includePatterns;
        }

        public List<String> getExcludePatterns() {
            return excludePatterns;
        }

        public void setExcludePatterns(List<String> excludePatterns) {
            this.excludePatterns = excludePatterns;
        }

        public String getHeaderName() {
            return headerName;
        }

        public void setHeaderName(String headerName) {
            this.headerName = headerName;
        }

        public String getParamName() {
            return paramName;
        }

        public void setParamName(String paramName) {
            this.paramName = paramName;
        }

        public boolean isAllowParameter() {
            return allowParameter;
        }

        public void setAllowParameter(boolean allowParameter) {
            this.allowParameter = allowParameter;
        }

        public boolean isLogAccess() {
            return logAccess;
        }

        public void setLogAccess(boolean logAccess) {
            this.logAccess = logAccess;
        }

        public boolean isRateLimitEnabled() {
            return rateLimitEnabled;
        }

        public void setRateLimitEnabled(boolean rateLimitEnabled) {
            this.rateLimitEnabled = rateLimitEnabled;
        }

        public int getMaxRequestsPerMinute() {
            return maxRequestsPerMinute;
        }

        public void setMaxRequestsPerMinute(int maxRequestsPerMinute) {
            this.maxRequestsPerMinute = maxRequestsPerMinute;
        }
    }

    /**
     * Web管理界面配置类
     */
    public static class Web {
        /**
         * 是否启用Web管理界面
         */
        private boolean enabled = true;

        /**
         * Web界面访问路径前缀
         */
        private String contextPath = "/apikey-admin";

        /**
         * 静态资源缓存时间（秒）
         */
        private int cachePeriod = 3600;

        /**
         * 是否启用CSRF保护
         */
        private boolean csrfEnabled = false;

        /**
         * 允许访问的IP地址列表
         */
        private List<String> allowedIps = new ArrayList<>();

        // Getters and Setters
        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public String getContextPath() {
            return contextPath;
        }

        public void setContextPath(String contextPath) {
            this.contextPath = contextPath;
        }

        public int getCachePeriod() {
            return cachePeriod;
        }

        public void setCachePeriod(int cachePeriod) {
            this.cachePeriod = cachePeriod;
        }

        public boolean isCsrfEnabled() {
            return csrfEnabled;
        }

        public void setCsrfEnabled(boolean csrfEnabled) {
            this.csrfEnabled = csrfEnabled;
        }

        public List<String> getAllowedIps() {
            return allowedIps;
        }

        public void setAllowedIps(List<String> allowedIps) {
            this.allowedIps = allowedIps;
        }
    }

    /**
     * 管理接口配置类
     */
    public static class Management {
        /**
         * 是否启用管理接口
         */
        private boolean enabled = true;

        /**
         * 管理接口路径前缀
         */
        private String basePath = "/api";

        /**
         * 是否需要认证
         */
        private boolean authRequired = false;

        /**
         * 管理员API Key
         */
        private String adminApiKey;

        /**
         * 是否启用跨域支持
         */
        private boolean corsEnabled = true;

        /**
         * 允许的跨域来源
         */
        private List<String> allowedOrigins = new ArrayList<String>() {{
            add("http://localhost:*");
            add("http://127.0.0.1:*");
            add("https://localhost:*");
            add("https://127.0.0.1:*");
        }};

        // Getters and Setters
        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public String getBasePath() {
            return basePath;
        }

        public void setBasePath(String basePath) {
            this.basePath = basePath;
        }

        public boolean isAuthRequired() {
            return authRequired;
        }

        public void setAuthRequired(boolean authRequired) {
            this.authRequired = authRequired;
        }

        public String getAdminApiKey() {
            return adminApiKey;
        }

        public void setAdminApiKey(String adminApiKey) {
            this.adminApiKey = adminApiKey;
        }

        public boolean isCorsEnabled() {
            return corsEnabled;
        }

        public void setCorsEnabled(boolean corsEnabled) {
            this.corsEnabled = corsEnabled;
        }

        public List<String> getAllowedOrigins() {
            return allowedOrigins;
        }

        public void setAllowedOrigins(List<String> allowedOrigins) {
            this.allowedOrigins = allowedOrigins;
        }
    }

    /**
     * 安全配置类
     */
    public static class Security {
        /**
         * 是否启用安全功能
         */
        private boolean enabled = true;

        /**
         * 最大失败尝试次数
         */
        private int maxFailedAttempts = 5;

        /**
         * 锁定持续时间（毫秒）
         */
        private long lockoutDuration = 300000; // 5分钟

        /**
         * 是否启用速率限制
         */
        private boolean rateLimitEnabled = false;

        /**
         * 每分钟最大请求数
         */
        private int maxRequestsPerMinute = 60;

        /**
         * 是否启用IP白名单
         */
        private boolean ipWhitelistEnabled = false;

        /**
         * 允许的IP地址列表
         */
        private List<String> allowedIps = new ArrayList<>();

        /**
         * API Key最小长度
         */
        private int minKeyLength = 32;

        /**
         * API Key最大长度
         */
        private int maxKeyLength = 64;

        /**
         * API Key默认有效期（天）
         */
        private int defaultExpiryDays = 365;

        /**
         * 是否启用API Key加密存储
         */
        private boolean encryptKeys = false;

        /**
         * 加密密钥
         */
        private String encryptionKey;

        // Getters and Setters
        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public int getMaxFailedAttempts() {
            return maxFailedAttempts;
        }

        public void setMaxFailedAttempts(int maxFailedAttempts) {
            this.maxFailedAttempts = maxFailedAttempts;
        }

        public long getLockoutDuration() {
            return lockoutDuration;
        }

        public void setLockoutDuration(long lockoutDuration) {
            this.lockoutDuration = lockoutDuration;
        }

        public boolean isRateLimitEnabled() {
            return rateLimitEnabled;
        }

        public void setRateLimitEnabled(boolean rateLimitEnabled) {
            this.rateLimitEnabled = rateLimitEnabled;
        }

        public int getMaxRequestsPerMinute() {
            return maxRequestsPerMinute;
        }

        public void setMaxRequestsPerMinute(int maxRequestsPerMinute) {
            this.maxRequestsPerMinute = maxRequestsPerMinute;
        }

        public boolean isIpWhitelistEnabled() {
            return ipWhitelistEnabled;
        }

        public void setIpWhitelistEnabled(boolean ipWhitelistEnabled) {
            this.ipWhitelistEnabled = ipWhitelistEnabled;
        }

        public List<String> getAllowedIps() {
            return allowedIps;
        }

        public void setAllowedIps(List<String> allowedIps) {
            this.allowedIps = allowedIps;
        }

        public int getMinKeyLength() {
            return minKeyLength;
        }

        public void setMinKeyLength(int minKeyLength) {
            this.minKeyLength = minKeyLength;
        }

        public int getMaxKeyLength() {
            return maxKeyLength;
        }

        public void setMaxKeyLength(int maxKeyLength) {
            this.maxKeyLength = maxKeyLength;
        }

        public int getDefaultExpiryDays() {
            return defaultExpiryDays;
        }

        public void setDefaultExpiryDays(int defaultExpiryDays) {
            this.defaultExpiryDays = defaultExpiryDays;
        }

        public boolean isEncryptKeys() {
            return encryptKeys;
        }

        public void setEncryptKeys(boolean encryptKeys) {
            this.encryptKeys = encryptKeys;
        }

        public String getEncryptionKey() {
            return encryptionKey;
        }

        public void setEncryptionKey(String encryptionKey) {
            this.encryptionKey = encryptionKey;
        }
    }

    /**
     * 监控配置类
     */
    public static class Monitoring {
        /**
         * 是否启用监控
         */
        private boolean enabled = false;

        /**
         * 监控数据收集间隔（秒）
         */
        private int collectInterval = 60;

        /**
         * 监控数据保留天数
         */
        private int retentionDays = 30;

        /**
         * 是否启用性能监控
         */
        private boolean performanceEnabled = true;

        /**
         * 是否启用使用统计
         */
        private boolean usageStatsEnabled = true;

        /**
         * 是否启用错误监控
         */
        private boolean errorMonitoringEnabled = true;

        /**
         * 监控端点路径
         */
        private String endpointPath = "/actuator/apikey-metrics";

        // Getters and Setters
        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public int getCollectInterval() {
            return collectInterval;
        }

        public void setCollectInterval(int collectInterval) {
            this.collectInterval = collectInterval;
        }

        public int getRetentionDays() {
            return retentionDays;
        }

        public void setRetentionDays(int retentionDays) {
            this.retentionDays = retentionDays;
        }

        public boolean isPerformanceEnabled() {
            return performanceEnabled;
        }

        public void setPerformanceEnabled(boolean performanceEnabled) {
            this.performanceEnabled = performanceEnabled;
        }

        public boolean isUsageStatsEnabled() {
            return usageStatsEnabled;
        }

        public void setUsageStatsEnabled(boolean usageStatsEnabled) {
            this.usageStatsEnabled = usageStatsEnabled;
        }

        public boolean isErrorMonitoringEnabled() {
            return errorMonitoringEnabled;
        }

        public void setErrorMonitoringEnabled(boolean errorMonitoringEnabled) {
            this.errorMonitoringEnabled = errorMonitoringEnabled;
        }

        public String getEndpointPath() {
            return endpointPath;
        }

        public void setEndpointPath(String endpointPath) {
            this.endpointPath = endpointPath;
        }
    }

    /**
     * 数据库配置类
     */
    public static class Database {
        /**
         * 是否自动创建表结构
         */
        private boolean autoCreateTables = true;

        /**
         * 是否初始化示例数据
         */
        private boolean initSampleData = false;

        /**
         * 表名前缀
         */
        private String tablePrefix = "";

        /**
         * 是否启用软删除
         */
        private boolean softDeleteEnabled = true;

        /**
         * 历史数据保留天数
         */
        private int historyRetentionDays = 90;

        /**
         * 访问日志保留天数
         */
        private int accessLogRetentionDays = 30;

        /**
         * 是否启用数据库连接池监控
         */
        private boolean poolMonitoringEnabled = false;

        // Getters and Setters
        public boolean isAutoCreateTables() {
            return autoCreateTables;
        }

        public void setAutoCreateTables(boolean autoCreateTables) {
            this.autoCreateTables = autoCreateTables;
        }

        public boolean isInitSampleData() {
            return initSampleData;
        }

        public void setInitSampleData(boolean initSampleData) {
            this.initSampleData = initSampleData;
        }

        public String getTablePrefix() {
            return tablePrefix;
        }

        public void setTablePrefix(String tablePrefix) {
            this.tablePrefix = tablePrefix;
        }

        public boolean isSoftDeleteEnabled() {
            return softDeleteEnabled;
        }

        public void setSoftDeleteEnabled(boolean softDeleteEnabled) {
            this.softDeleteEnabled = softDeleteEnabled;
        }

        public int getHistoryRetentionDays() {
            return historyRetentionDays;
        }

        public void setHistoryRetentionDays(int historyRetentionDays) {
            this.historyRetentionDays = historyRetentionDays;
        }

        public int getAccessLogRetentionDays() {
            return accessLogRetentionDays;
        }

        public void setAccessLogRetentionDays(int accessLogRetentionDays) {
            this.accessLogRetentionDays = accessLogRetentionDays;
        }

        public boolean isPoolMonitoringEnabled() {
            return poolMonitoringEnabled;
        }

        public void setPoolMonitoringEnabled(boolean poolMonitoringEnabled) {
            this.poolMonitoringEnabled = poolMonitoringEnabled;
        }
    }
}